/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.dao.impl;

import cz.muni.fi.pa165.bookingmanager.dao.CustomerManager;
import cz.muni.fi.pa165.bookingmanager.entities.Customer;
import cz.muni.fi.pa165.bookingmanager.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.bookingmanager.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.bookingmanager.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Radek Koubský
 */
@Stateless
public class CustomerManagerImpl implements Serializable, CustomerManager {
    private static final long serialVersionUID = 1L;

    @PersistenceContext(unitName = "BookingManagerPU")
    private EntityManager em;

    @Override
    public void createCustomer(Customer customer) throws IllegalOrphanException, PreexistingEntityException {
        if (customer == null) {
            throw new IllegalArgumentException();
        }

        List<String> illegalOrphanMessages = null;
        if (customer.getId() != null) {
            illegalOrphanMessages = new ArrayList<String>();
            illegalOrphanMessages.add("Customer id is not null, Customer cant be added to database ");
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        if (customer.getFirstName().equals("")) {
            throw new EJBException("name should not be null");
        }
        if (customer.getNumberOfIdCard().equals("")) {
            throw new EJBException("num of id card should not be null");
        }
        if (customer.getSurName().equals("")) {
            throw new EJBException("surname should not be null");
        }
        em.persist(customer);
    }

    @Override
    public void updateCustomer(Customer customer) throws IllegalOrphanException, PreexistingEntityException, NonexistentEntityException, Exception {
        if (customer == null) {
            throw new IllegalArgumentException();
        }

        try {
            List<String> illegalOrphanMessages = null;
            if (customer.getId() == null) {
                illegalOrphanMessages = new ArrayList<String>();
                illegalOrphanMessages.add("Customer id is null");
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (customer.getFirstName().equals("")) {
                throw new EJBException("name should not be null");
            }
            if (customer.getNumberOfIdCard().equals("")) {
                throw new EJBException("num of id card should not be null");
            }
            if (customer.getSurName().equals("")) {
                throw new EJBException("surname should not be null");
            }
            em.merge(customer);

        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = customer.getId();
                if (getCustomerById(id) == null) {
                    throw new NonexistentEntityException("The customer with id " + id + " no longer exists.");
                }
            }
            throw ex;
        }
    }

    @Override
    public void deleteCustomer(Customer customer) throws NonexistentEntityException, IllegalOrphanException {
        if (customer == null) {
            throw new IllegalArgumentException("Customer is null");
        }

        List<String> illegalOrphanMessages = null;
        if (customer.getId() == null) {
            illegalOrphanMessages = new ArrayList<String>();
            illegalOrphanMessages.add("customer id is null");
            throw new IllegalOrphanException(illegalOrphanMessages);
        }

        try {
            em.remove(em.merge(customer));
            
        } catch (IllegalArgumentException ex) {
            throw new NonexistentEntityException("The customer no longer exists.");
        }
    }

    @Override
    public Customer getCustomerById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException();
        }
        return em.find(Customer.class, id);
    }

    @Override
    public List<Customer> findCustomersByName(String customer) {

        Query q = em.createQuery("select object(o) from Customer as o where o.surName='" + customer + "'");
        return q.getResultList();

    }

    @Override
    public List<Customer> findAllCustomers() {
        Query q = em.createQuery("select object(o) from Customer as o");
        return q.getResultList();
    }
}
